Skip to content

vm: allow createCachedData() in any module status - #67508

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
magurotuna:vm-createcacheddata-after-eval
Nov 29, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
magurotuna:vm-createcacheddata-after-eval

Conversation

@magurotuna

@magurotuna magurotuna commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

sourceTextModule.createCachedData() throws ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA if the module is already evaluated. This check came from #31278, which matched a V8 check at that time. V8 removed its check in https://crrev.com/c/2504256 in Nov 2020, but Node has not caught up with it. So this PR allows the call to createCachedData() for any status.

This will help speed up the vm pools of Vitest. A code cache created after evaluation can also contain the functions that V8 compiled lazily, so a module that uses it can compile less. A Vitest prototype that uses cached data after evaluation in subsequent test files took 12-24% less wall-clock time on a 500-file benchmark.

I used an AI coding assistant to help write the patch and the tests. I reviewed them.

Refs: https://chromium-review.googlesource.com/c/v8/v8/+/2504256
Refs: https://chromium-review.googlesource.com/c/v8/v8/+/8811778

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/vm

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. errors Issues and PRs related to JavaScript errors originating in Node.js core. needs-ci PRs that need a full CI run. vm Issues and PRs related to the vm subsystem. labels Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

@magurotuna
magurotuna force-pushed the vm-createcacheddata-after-eval branch from 2f22f01 to ad44309 Compare November 16, 2026 07:43
@magurotuna
magurotuna marked this pull request as ready for review November 16, 2026 07:47
@magurotuna

Copy link
Copy Markdown
Contributor Author

The V8 change that fixes the stale comment of v8::Module::GetUnboundModuleScript() has landed: https://crrev.com/c/8811778

@inoway46

Copy link
Copy Markdown
Contributor

I checked that Node's bundled V8 already supports this: GetUnboundModuleScript() no longer rejects evaluated modules, and GetSharedFunctionInfo() handles that state. No V8 backport appears necessary.

@inoway46

Copy link
Copy Markdown
Contributor

FYI, the coverage-windows CI failure should be fixed by c84f614, so rebasing onto the latest main will make it pass.

@magurotuna
magurotuna force-pushed the vm-createcacheddata-after-eval branch from ad44309 to cc9cecf Compare November 19, 2026 17:46
@magurotuna

Copy link
Copy Markdown
Contributor Author

@inoway46 Thanks for the info! rebased

@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.51%. Comparing base (c58ccfa) to head (5bc14b8).
⚠️ Report is 39 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67508      +/-   ##
==========================================
- Coverage   90.52%   90.51%   -0.02%     
==========================================
  Files         805      805              
  Lines      285669   286357     +688     
  Branches    55151    55324     +173     
==========================================
+ Hits       258616   259192     +576     
- Misses      17173    17248      +75     
- Partials     9880     9917      +37     
Files with missing lines Coverage Δ
lib/internal/errors.js 98.18% <ø> (+0.05%) ⬆️
lib/internal/vm/module.js 96.33% <100.00%> (-0.02%) ⬇️
src/module_wrap.cc 73.76% <ø> (-0.13%) ⬇️

... and 53 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@MikeMcC399

This comment was marked as resolved.

@magurotuna
magurotuna force-pushed the vm-createcacheddata-after-eval branch from cc9cecf to 4dba7b7 Compare November 28, 2026 04:16
@magurotuna

Copy link
Copy Markdown
Contributor Author

@MikeMcC399 Rebased. Thanks!

@inoway46

Copy link
Copy Markdown
Contributor

The code and tests look good to me for the intended change. The check introduced in #31278 matched V8’s restriction at the time, but V8 now handles both evaluating and errored. With the checks removed locally, I could reuse a cache from each state in a fresh process. Is there a reason to keep rejecting createCachedData() in those states?

Sorry I only noticed this after the rebase and CI rerun.

@magurotuna magurotuna changed the title vm: allow createCachedData() after module evaluation vm: allow createCachedData() in any module status Nov 29, 2026
@inoway46

Copy link
Copy Markdown
Contributor

LGTM. Before we rerun CI, could you squash the three commits into one and update the commit message and PR description to say that createCachedData() can now be called in any module state? That way, the commit queue can land it with the correct message.

@magurotuna

Copy link
Copy Markdown
Contributor Author

@inoway46 Thanks! I just updated the PR description to correctly reflect the current implementation

sourceTextModule.createCachedData() threw
ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA after the module started
evaluation. V8 removed this restriction in 2020, and it handles
every module status when it creates a code cache. A code cache
that is created after evaluation can also contain the functions
that V8 compiled lazily during the evaluation, so a new module
that uses this code cache can compile less.

Allow the call in any module status, and remove
ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA, which is no longer
thrown. Throw ERR_INVALID_THIS if the receiver is not a
SourceTextModule. A SyntheticModule receiver could reach a CHECK
in ModuleWrap::CreateCachedData() and abort the process.

Assisted-by: claude:opus-5.5
Signed-off-by: Yusuke "YT" Tanaka <ytanaka@cloudflare.com>
@magurotuna
magurotuna force-pushed the vm-createcacheddata-after-eval branch from 592df7d to 5bc14b8 Compare November 29, 2026 03:55
@magurotuna

Copy link
Copy Markdown
Contributor Author

And squashed the commits into one with the right message

inoway46

This comment was marked as resolved.

@MikeMcC399 MikeMcC399 added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 29, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 29, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@inoway46 inoway46 added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 29, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 4b26c72 into nodejs:main Nov 29, 2026
68 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 4b26c72

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 29, 2026
@magurotuna
magurotuna deleted the vm-createcacheddata-after-eval branch November 30, 2026 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. c++ Issues and PRs that require attention from people who are familiar with C++. errors Issues and PRs related to JavaScript errors originating in Node.js core. needs-ci PRs that need a full CI run. vm Issues and PRs related to the vm subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants